package com.kexuexiong.demo.bear.controller.index;


import com.kexuexiong.demo.bear.common.dto.Result;
import com.kexuexiong.demo.bear.httpApi.HttpApi;
import com.kexuexiong.demo.bear.httpApi.dto.req.ZdzReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import retrofit2.Response;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Api(tags = {"首页"})
@RestController
@RequestMapping("/index")
@Slf4j
public class IndexController {

    @Value("${content.str}")
    private String content;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private HttpApi httpApi;


    /**
     * 首页测试
     *
     * @param name
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "没有")})
    @RequestMapping(value = "/index", method = {RequestMethod.GET})
    @ApiOperation(value = "首页测试", notes = "首页测试")
    public Result<String> index(@RequestParam("name") String name) {
        log.info("index,name=>{}", name);
        RLock fairLock = null;
        try {
            fairLock = redissonClient.getFairLock(name);
            boolean lock = fairLock.tryLock(2, 10, TimeUnit.SECONDS);
            if (!lock) {
                return Result.success("fuck man not get lock!");
            }
            Thread.sleep(5000);
            redisTemplate.opsForValue().set("name", content);
            return Result.success("index" + name + content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(fairLock) && fairLock.isLocked()) {
                fairLock.unlock();
            }
        }
        return Result.success("index" + name + content);
    }

    /**
     * retrofit测试
     *
     * @param name
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "没有")})
    @RequestMapping(value = "/retrofitTest", method = {RequestMethod.GET})
    @ApiOperation(value = "retrofitTest", notes = "retrofit测试")
    public Result<String> retrofitTest(@RequestParam("name") String name) {
        log.info("retrofitTest,name=>{}", name);
        Response<Result<String>> result = httpApi.getResult(1L);
        return Result.success(result);
    }

    /**
     * retrofit登录微信小程序
     *
     * @param phone
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "没有")})
    @RequestMapping(value = "/retrofitTestLoginByPhone", method = {RequestMethod.GET})
    @ApiOperation(value = "retrofitTestLoginByPhone", notes = "retrofit测试")
    public Result<Object> retrofitTestLoginByPhones(@RequestParam("phone") String phone) {
        log.info("retrofitTestLoginByPhones,phone=>{}", phone);
        Object result = httpApi.loginByPhone(new ZdzReq<Map<String, Object>>(new HashMap<String, Object>() {
            {
                put("phone", phone);
            }
        }));
        return Result.success(result);
    }
}
